<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\ZiliaoCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Role;
use Dcat\Admin\Show;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class ZiliaoCategoryController extends AdminController
{

    protected $title = '资料分类';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ZiliaoCategory(), function (Grid $grid) {

            $grid->disableViewButton();

            $grid->number('序号')->sortable();
            $grid->column('title');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ZiliaoCategory(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ZiliaoCategory(), function (Form $form) {
            $form->display('id');
            $form->text('title');

            $form->table('process','流程', function (NestedForm $table) {
                $table->select('type','行为')->options(['1'=>'发起','2'=>'审核','3'=>'归档']);
                $table->select('method','方式')->options(['1'=>'串行','2'=>'并行']);
                $table->select('step','步骤')->options(['1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10']);
                $table->select('role','角色')->options(
                    function (){
                        $result = Role::get(['slug','name']);
                        $list = array();
                        foreach ($result as $info){
                            $list[$info['slug']] = $info['name'];
                        }
                        return $list;
                    }
                );
            })->saving(function ($value) {
                // 转化成json字符串保存到数据库
                return json_encode($value,JSON_UNESCAPED_UNICODE);
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
